<%@page import="com.uhaou.system.model.*" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/admin/commons/taglibs.jsp" %>
<html>
<head> 
	<title>用户管理</title>
	<link type="text/css" rel="stylesheet" href="${ctx}/css/themes/default/easyui.css">
	<link type="text/css" rel="stylesheet" href="${ctx}/css/themes/icon.css">
	
	<script type="text/javascript" src="${ctx}/scripts/jquery-1.4.2.min.js"></script>
	<script type="text/javascript" src="${ctx}/scripts/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="${ctx}/scripts/locale/easyui-lang-zh_CN.js"></script>
		<script>
	$(function(){
		//树形控件功能
		$('#tt').tree({
			
			checkbox: false,
			url: '${ctx}/system/SysUser/create.do?param=json'
		});
		
		//角色datagrid 的加载 --初始化加载一次，后续不需要重新获得数据
		$('#roleDataGrid').datagrid({
				title:'角色',
				iconCls:'icon-role',
				width:580,//首次进入时候的长度，下面有resize重新设置了。
				height:300,
				nowrap: true,
				striped: true,//间变
				collapsible:true,//折叠按钮
				url:'${ctx}/system/role/TsysRole/listAjax.do',
				sortName: 'code',
				sortOrder: 'desc',
				remoteSort: false,
				idField:'id',
				frozenColumns:[[//冻结列
	                {field:'ck',checkbox:true},
	                {title:'角色名称',field:'roleName',width:150,sortable:true}//可排序
				]],
				columns:[[
					{title:'状态',field:'deleted',width:50,sortable:true,
						formatter:function(a,rec){
							if (a == 0){
							    return '<span style="color:red;">删除</span>';
							} else {
							    return "正常";
							}
						}
					},
					{title:'备注',field:'memo',width:250}
				]],
				toolbar:[{
					id:'btnsave',
					text:'提交',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						var selected = $('#roleDataGrid').datagrid('getSelected');
						
						var ids = [];
						var rows = $('#roleDataGrid').datagrid('getSelections');
						
						for(var i=0;i<rows.length;i++){
							ids.push(rows[i].id);
						}
						//alert(ids.join(':'));
						
						$('#btnsave').linkbutton('disable');
						
						
						var node = $('#tt').tree('getSelected');
						
						if(node.iconCls=="icon-user"){//节点是用户
							$("input[name='objType']").val(0);
						}else if(node.iconCls=="icon-dept"){//节点是部门
							$("input[name='objType']").val(1);
						}else if(node.iconCls=="icon-post"){//节点是岗位--目前没有岗位的角色
							$("input[name='objType']").val(2);
						}else{
							alert("分配角色失败，没有相关的对象类型！");
							return;
						}
						//关联表的授权对象ID就是tree 的node节点ID。。。
						$("input[name='objId']").val(node.id);
						$("input[name='roleId']").val(ids.join(':'));
						$('#roleForm').attr("action","${ctx }/system/role/TsysRoleAssign/save.do");
						$('#roleForm').submit();
						
						
					}
				}]
			});

		//提交表单Ajax操作
		$('#deptForm').form({
			 onSubmit:function(){
	     	   if( $(this).form('validate')){
		     	   //校验成功，把按钮失效
	     			$('#submit1').linkbutton({disabled: true});
	     			return true;
	     	   }else
		     	   return false;
	    	},
			success:function(data){
				if(data==1)	$('#tt').tree('reload');
			}
		});
		$('#userForm').form({
			 onSubmit:function(){
	     	   if( $(this).form('validate')){
		     	   //校验成功，把按钮失效
	     			$('#submit2').linkbutton({disabled: true});
	     			return true;
	     	   }else
		     	   return false;
	    	},
			success:function(data){
				if(data==1)$('#tt').tree('reload');
			}
		});
		
		$('#postForm').form({
			 onSubmit:function(){
	     	   if( $(this).form('validate')){
		     	   //校验成功，把按钮失效
	     			$('#submit3').linkbutton({disabled: true});
	     			return true;
	     	   }else
		     	   return false;
	    	},
			success:function(data){
				if(data==1)$('#tt').tree('reload');
			}
		});
		$('#roleForm').form({
			 onSubmit:function(){
	     	   if( $(this).form('validate')){
		     	   //校验成功，把按钮失效
	     			return true;
	     	   }else
		     	   return false;
	    	},
			success:function(data){
				if(data==1)$('#tt').tree('reload');
			}
		});
		
		//右键菜单功能 
		$(document).bind('contextmenu',function(e){
			$('#mm').menu('show', {
				left: e.pageX,
				top: e.pageY
			});
			return false;
		});

		$('#submit1').linkbutton({disabled: true});
		$('#submit2').linkbutton({disabled: true});
		$('#submit3').linkbutton({disabled: true});
		//$('#deptDiv').hide();
		$('#userDiv').hide();
		$('#postDiv').hide();
		$('#roleDiv').hide();
		$("#dept_deptCode").attr("readonly","readonly");
	});
	//删除菜单
	function delete1(){
		var node = $('#tt').tree('getSelected');
		//alert(node.iconCls);
		
		if(node==null){
			alert("请先选择结点");
			return;
		}
		
		if(node.id==-1)alert("不能删除顶级菜单");
		//判断所选的结点是部门还是岗位还是用户
		
		else{
			var messtr = '你确定要执行删除操作吗？';
			if(node.iconCls=="icon-dept"){//节点是部门
				messtr="删除部门会删除部门下的人员和部门，你确定要继续操作吗？";
			}
			
			$.messager.confirm('确认', messtr , function(r){
				if (r){
					if(node.iconCls=="icon-user"){//节点是用户
				
						$.ajax({ url: "${ctx}/system/SysUser/deletebyid.do?id="+node.id, context: document.body, success: function(){
						$.messager.alert('删除操作','删除成功!');
						}});
						
					}else if(node.iconCls=="icon-dept"){//节点是部门
						
						$.ajax({ url: "${ctx}/system/dept/TsysDept/deletebyid.do?id="+node.id, context: document.body, success: function(){
						$.messager.alert('删除操作','删除成功!');
						}});
					}else if(node.iconCls=="icon-post"){//节点是岗位
						
						$.ajax({ url: "${ctx}/system/post/TsysPost/deletebyid.do?id="+node.id, context: document.body, success: function(){
						$.messager.alert('删除操作','删除成功!');
						}});
					}
					
					//把父结点重新展开一下，解决checkbox显示不准确bug
					var parent=$('#tt').tree('getParent',node.target);
					$('#tt').tree('remove', node.target);
					$('#tt').tree('expand',parent.target);
					$('#tt').tree('reload');
				}
			});
			
			
		
			
			
			
		}
	}
	//编辑菜单 
	function edit(){
		var node = $('#tt').tree('getSelected');
		
		if(node==null){
			alert("请先选择结点");
			return;
		}
		
		
		if(node.id==-1)alert("不能修改顶级菜单");
		//判断所选的结点是部门还是岗位还是用户
		
		
		
		else{
			//alert(node.iconCls);
			if(node.iconCls=="icon-user"){//节点是用户
				$("#deptDiv").hide();
				$("#userDiv").show();
				$('#postDiv').hide();
				$('#roleDiv').hide();
				
				$("#userForm").form("load",{
					id : node.id,
					deptId : node.attributes['deptId'],
					deleted : node.attributes['deleted'],
					userName : node.attributes['userName'],
					userPwd : node.attributes['userPwd'],
					cardType : node.attributes['cardType'],
					cardId : node.attributes['cardId'],
					company : node.attributes['company'],
					userNo : node.attributes['userNo'],
					companyAdd : node.attributes['companyAdd'],
					birthday : node.attributes['birthday'],
					email : node.attributes['email'],
					phone : node.attributes['phone'],
					mobile : node.attributes['mobile'],
					familyAddr : node.attributes['familyAddr'],
					sex : node.attributes['sex'],
					fax : node.attributes['fax'],
					creator : node.attributes['creator'],
					createTime : node.attributes['createTime'],
					userStatus : node.attributes['userStatus'],
					memo : node.attributes['memo']
				});
				
				
				//动态改变form 的提交路径。。此为增加方法
				//document.getElementById("userForm").action="${ctx}/orgn/OrgUser/update.do";
				$('#userForm').attr("action","${ctx}/system/SysUser/update.do");
				
				$('#submit2').linkbutton({disabled:false});
			}else if(node.iconCls=="icon-dept"){//节点是部门
				$("#userDiv").hide();
				$("#deptDiv").show();
				$('#postDiv').hide();
				$('#roleDiv').hide();
				
				//alert(node.attributes['deleted']);
				
				$("#dept_id").val(node.id);
				$("#dept_companyId").val(node.attributes['companyId']);
				$("#dept_parentDeptid").val(node.attributes['parentDeptid']);
				$("#dept_deleted").val(node.attributes['deleted']);
				$("#dept_deptName").val(node.attributes['deptName']);
				$("#dept_deptEmail").val(node.attributes['deptEmail']);
				$("#dept_deptFax").val(node.attributes['deptFax']);
				$("#dept_deptManager").val(node.attributes['deptManager']);
				$("#dept_deptPhone").val(node.attributes['deptPhone']);
				$("#dept_orderCode").val(node.attributes['orderCode']);
				$("#dept_regionFlag").val(node.attributes['regionFlag']);
				$("#dept_deptCode").val(node.attributes['deptCode']);
				$("#dept_memo").val(node.attributes['memo']);
				$("#dept_creator").val(node.attributes['creator']);
				$("#dept_createTime").val(node.attributes['createDateString']);
				
				
				//动态改变form 的提交路径。。此为修改方法
				//document.getElementById("deptForm").action="${ctx }/OrgDepartment/update.do";
				$('#deptForm').attr("action","${ctx}/system/dept/TsysDept/update.do");
				$('#submit1').linkbutton({disabled:false});
			}else if(node.iconCls=="icon-post"){//节点是岗位
				$("#userDiv").hide();
				$("#deptDiv").hide();
				$('#postDiv').show();
				$('#roleDiv').hide();
				//alert(node.id+"-------"+node.attributes['deptId']);
				
				
				$("input[name='postId']").val(node.id);
				$("input[name='deptId']").val(node.attributes['deptId']);
				$("input[name='postName']").val(node.attributes['postName']);
				$("input[name='postOrder']").val(node.attributes['postOrder']);
				$("input[name='postDesc']").val(node.attributes['postDesc']);
				
				//动态改变form 的提交路径。。此为修改方法
				//document.getElementById("deptForm").action="${ctx }/OrgDepartment/update.do";
				$('#postForm').attr("action","${ctx}/system/post/TsysPost/update.do");
				$('#submit3').linkbutton({disabled:false});
			}
			
		}
	}

	//增加部门
	function addDept(){
		
		var node = $('#tt').tree('getSelected');
		if(node==null){
			alert("请先选择结点");
			return;
		}
		var children = $('#tt').tree('getChildren',node.target);
		
		if(node.iconCls=="icon-user"){//节点是用户
			$.messager.alert("增加操作","不能在用户下增加部门！");
			return ;
		}
		if(node.iconCls=="icon-post"){//节点是岗位
			$.messager.alert("增加操作","不能在岗位下增加部门！");
			return ;
		}
		
		$("#userDiv").hide();
		$("#deptDiv").show();
		$('#postDiv').hide();
		$('#roleDiv').hide();
		
		//alert(node.id+"-----"+node.attributes['deptCode']);
		
		$.ajax({ url: "${ctx}/system/dept/TsysDept/getCode.do?id="+node.id+"&deptCode="+node.attributes['deptCode'], context: document.body, success: function(msg){
			//$.messager.alert('增加部门',msg);
			if(msg=="false"){
				$.messager.alert('',"生成编码失败");
				return;
			}else{
				$("#dept_id").val("");
				$("#dept_companyId").val("-1");
				$("#dept_parentDeptid").val(node.id);
				$("#dept_deleted").val(0);
				$("#dept_deptName").val("");
				$("#dept_deptEmail").val("");
				$("#dept_deptFax").val("");
				$("#dept_deptManager").val("");
				$("#dept_deptPhone").val("");
				$("#dept_orderCode").val("");
				$("#dept_regionFlag").val("");
				$("#dept_deptCode").val(msg);
				
				$("#dept_memo").val("");
				$("#dept_creator").val("");
				$("#dept_createTime").val("");
				
				//动态改变form 的提交路径。。此为增加方法
				//document.getElementById("deptForm").action="${ctx }/orgn/OrgDepartment/save.do";
				$('#deptForm').attr("action","${ctx}/system/dept/TsysDept/save.do");
				$('#submit1').linkbutton({disabled:false});
			}
		}});
		
		
	}

	//增加用户
	function addUser(){
		
		var node = $('#tt').tree('getSelected');
		if(node==null){
			alert("请先选择结点");
			return;
		}
		
		if(node.iconCls=="icon-user"){//节点是用户
			$.messager.alert("增加操作","不能在用户下增加用户！");
			return ;
		}
		
		$("#deptDiv").hide();
		$("#userDiv").show();
		$('#postDiv').hide();
		$('#roleDiv').hide();
		
		var children = $('#tt').tree('getChildren',node.target);
		
		$("#userForm").form("load",{
			id:"",
			deptId:node.id,
			deleted:"0",
			userName:"",
			userPwd:"",
			cardType:"",
			cardId:"",
			company:"",
			userNo:"",
			companyAdd:"",
			birthday:"",
			email:"",
			phone:"",
			mobile:"",
			familyAddr:"",
			sex:"",
			fax:"",
			creator:"",
			createTime:"",
			userStatus:"",
			memo:""
		});
		
		//动态改变form 的提交路径。。此为增加方法
		//document.getElementById("userForm").action="${ctx}/orgn/OrgUser/save.do";
		$('#userForm').attr("action","${ctx}/system/SysUser/save.do");
		$('#submit2').linkbutton({disabled:false});
	}
	
	//增加岗位
	function addPost(){
		
		var node = $('#tt').tree('getSelected');
		if(node==null){
			alert("请先选择结点");
			return;
		}
		
		if(node.iconCls=="icon-user"){//节点是用户
			$.messager.alert("增加操作","不能在用户下增加岗位！");
			return ;
		}
		if(node.iconCls=="icon-post"){//节点是岗位
			$.messager.alert("增加操作","不能在岗位下增加岗位！");
			return ;
		}
		
		$("#deptDiv").hide();
		$("#userDiv").hide();
		$('#postDiv').show();
		$('#roleDiv').hide();
		
		var children = $('#tt').tree('getChildren',node.target);
		$("input[name='deptId']").val(node.id);
		$("input[name='postId']").val("");
		$("input[name='postName']").val("");
		$("input[name='postOrder']").val(children.length+1);
		$("input[name='postDesc']").val("");
		
		//动态改变form 的提交路径。。此为增加方法
		//document.getElementById("userForm").action="${ctx}/orgn/OrgUser/save.do";
		$('#postForm').attr("action","${ctx}/system/post/TsysPost/save.do");
		$('#submit3').linkbutton({disabled:false});
	}
	
	//分配角色
	function addRole(){
		
		
		var node = $('#tt').tree('getSelected');
		//alert(node.iconCls);
		
		if(node==null){
			alert("请先选择结点");
			return;
		}
		if(node.id==-1){
			alert("不能给顶级菜单分配角色");
			return ;
		}
		
		$("#deptDiv").hide();
		$("#userDiv").hide();
		$('#postDiv').hide();
		$('#roleDiv').show();
		$('#roleDataGrid').datagrid('resize', {
				width:580,
				height:300
			});
		
		
		$('#roleDataGrid').datagrid('clearSelections');//清空选择项
		var tsstr = node.attributes['tsysRoleAssign'];
		for (var v=0;v<tsstr.split(":").length;v++){
			$('#roleDataGrid').datagrid('selectRecord',tsstr.split(":")[v]);//重新默认选择
		}
		
		
		$('#btnsave').linkbutton({disabled: false});
	}
	
	
</script>
</head>
<body>
	<s:form action="/system/SysUser/save.do" method="post">
		<input id="submitButton" name="submitButton" type="submit" value="提交" />
		<input type="button" value="返回列表" onclick="window.location='${ctx}/system/SysUser/list.do'"/>
		<input type="button" value="后退" onclick="history.back();"/>
		
		<form id="form1" action="${ctx}/system/SysUser/save.do" method="post">
		<input type="submit" value="提交" />
		<input type="button" value="返回列表" onclick="window.location='${ctx}/system/SysUser/list.do'"/>
		<input type="button" value="后退" onclick="history.back();"/>
		
		<table class="formTable">
			<input type="hidden" id="id" name="id" value="${model.id}"/>
		
		<!-- ONGL access static field: @package.class@field or @vs@field -->
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_ACCOUNT_NAME%>:
				</td>	
				<td>
				<input id="accountName" name="accountName" value="${model.accountName}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_USER_NAME%>:
				</td>	
				<td>
				<input id="userName" name="userName" value="${model.userName}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_USER_PWD%>:
				</td>	
				<td>
				<input id="userPwd" name="userPwd" value="${model.userPwd}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_CARD_TYPE%>:
				</td>	
				<td>
				<input id="cardType" name="cardType" value="${model.cardType}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_CARD_ID%>:
				</td>	
				<td>
				<input id="cardId" name="cardId" value="${model.cardId}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_COMPANY%>:
				</td>	
				<td>
				<input id="company" name="company" value="${model.company}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_USER_NO%>:
				</td>	
				<td>
				<input id="userNo" name="userNo" value="${model.userNo}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_COMPANY_ADD%>:
				</td>	
				<td>
				<input id="companyAdd" name="companyAdd" value="${model.companyAdd}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_DEPT_ID%>:
				</td>	
				<td>
				<input id="deptId" name="deptId" value="${model.deptId}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_BIRTHDAY%>:
				</td>	
				<td>
				<input id="birthday" name="birthday" value="${model.birthday}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_EMAIL%>:
				</td>	
				<td>
				<input id="email" name="email" value="${model.email}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_PHONE%>:
				</td>	
				<td>
				<input id="phone" name="phone" value="${model.phone}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_MOBILE%>:
				</td>	
				<td>
				<input id="mobile" name="mobile" value="${model.mobile}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_FAMILY_ADDR%>:
				</td>	
				<td>
				<input id="familyAddr" name="familyAddr" value="${model.familyAddr}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_SEX%>:
				</td>	
				<td>
				<input id="sex" name="sex" value="${model.sex}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_FAX%>:
				</td>	
				<td>
				<input id="fax" name="fax" value="${model.fax}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_CREATOR%>:
				</td>	
				<td>
				<input id="creator" name="creator" value="${model.creator}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_CREATE_TIME%>:
				</td>	
				<td>
				<input value="${model.createTimeString}" id="createTimeString" name="createTimeString"  
				type="text" class="easyui-datebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_USER_STATUS%>:
				</td>	
				<td>
				<input id="userStatus" name="userStatus" value="${model.userStatus}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_DELETED%>:
				</td>	
				<td>
				<input id="deleted" name="deleted" value="${model.deleted}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
			
			
			<tr>	
				<td class="tdLabel">
					<%=SysUser.ALIAS_MEMO%>:
				</td>	
				<td>
				<input id="memo" name="memo" value="${model.memo}"
				type="text"  class="easyui-validatebox"   />
				</td>
			</tr>
		</table>
	</form>
	</s:form>
	
	<script>
		
		new Validation(document.forms[0],{onSubmit:true,onFormValidate : function(result,form) {
			var finalResult = result;
			
			//在这里添加自定义验证
			
			return disableSubmit(finalResult,'submitButton');
		}});
	</script>
</body>
</html>
